//
//  Globle.m
//  TBRJL
//
//  Created by 程三 on 15/6/13.
//  Copyright (c) 2015年 程三. All rights reserved.
//

#import "Globle.h"
#define UserInfoKEY @"UserInfoKEY"
#define IsNormal @"IsNormal"
#define AreaidKEY @"AreaidKEY"
#define LocationKEY @"LocationKEY"
#define HomeHeadlineKEY @"HomeHeadlineKEY"
#define HeadlineKEY @"HeadlineKEY"
#define PolicyKEY @"PolicyKEY"
#define FJKEY @"FJKEY"

static Globle *globle;
@implementation Globle

-(id)init
{
    self = [super init];
    if(self)
    {
        //获取用户信息
        NSDictionary *tempDic = [Util getDataForKey:UserInfoKEY];
        if(tempDic != nil)
        {
            self.account = [AccountModel mj_objectWithKeyValues:tempDic];
            self.headImg = [Tools resolutionHeadImage:self.account.headimg];
            if(![[tempDic allKeys] containsObject:@"leapuserid"])
            {
                self.isNormal = YES;
            }
            else{
                self.isNormal = NO;
            }
        }
        else {
            self.isNormal = YES;
        }
        //区域id
        NSString *areaidStr = [Util getDataForKey:AreaidKEY];
        if (areaidStr.length) {
            self.areaid = areaidStr;
        }
        //获取定位信息
        NSDictionary *locationDic = [Util getDataForKey:LocationKEY];
        if(locationDic != nil)
        {
            self.locations = [LocationModel mj_objectWithKeyValues:locationDic];
        }
        //获取首页新闻
        NSArray *homeArys = [Util getDataForKey:HomeHeadlineKEY];
        if (homeArys.count) {
            self.homeHeadlineArys = [VillageModel mj_objectArrayWithKeyValuesArray:homeArys];
        }
        //获取村头条
        NSArray *headline = [Util getDataForKey:HeadlineKEY];
        if(headline.count)
        {
            self.headlineArys = headline;
        }
        //获取政策法规
        NSArray *policyArys = [Util getDataForKey:PolicyKEY];
        if (policyArys.count) {
            self.policyArys = [PolicyModel mj_objectArrayWithKeyValuesArray:policyArys];
        }
    }
    
    return  self;
}

+(Globle *)getInstance
{
    //加锁
    @synchronized(self)
    {
        if(globle == nil)
        {
            globle = [[[self class] alloc] init];
        }
    }
    return globle;
}

//清楚所有数据
- (void)cleanAllObject
{
    [Util removeAllUserDefaults];
    [Util saveNSUserDefaultsForObject:[NSDictionary dictionary] forKey:UserInfoKEY];
    self.account = nil;
    self.headImg = nil;
    self.visitorIcon = nil;
    self.visitorName = nil;
    self.visitorQQID = nil;
    self.visitorWXID = nil;
}

//用户信息
- (void)saveAccountInfo:(NSDictionary *) dict
{
    if (![[dict allKeys] count]) {
        return;
    }
    self.account = [AccountModel mj_objectWithKeyValues:dict];
    [Util saveNSUserDefaultsForObject:dict forKey:UserInfoKEY];
    
    self.headImg = [Tools resolutionHeadImage:self.account.headimg];
    
    if(![[dict allKeys] containsObject:@"leapuserid"])
    {
        self.isNormal = YES;
    }
    else{
        self.isNormal = NO;
    }
    [Util saveNSUserDefaultsForBOOL:self.isNormal forKey:IsNormal];
}

//位置信息
- (void)saveAreaid:(NSString *)areaidStr
{
    if (areaidStr == nil || [areaidStr isEqualToString:@""]) {
        return;
    }
    self.areaid = areaidStr;
    [Util saveNSUserDefaultsForObject:areaidStr forKey:AreaidKEY];
}

//定位信息
- (void)saveLocation:(LocationModel *) model
{
    if (model == nil) {
        return;
    }
    self.locations = model;
    [Util saveNSUserDefaultsForObject:model.mj_keyValues forKey:LocationKEY];
}

//首页头条
- (void)saveHomeHeadline:(NSArray *)arrays
{
    if (!arrays.count) {
        return;
    }
    self.homeHeadlineArys = [VillageModel mj_objectArrayWithKeyValuesArray:arrays];
    [Util saveNSUserDefaultsForObject:arrays forKey:HomeHeadlineKEY];
}

//村头条
- (void)saveHeadline:(NSArray *)arrays
{
    if (![arrays count]) {
        return;
    }
    self.headlineArys = arrays;
    [Util saveNSUserDefaultsForObject:arrays forKey:HeadlineKEY];
}

//政策法规
- (void)savePolicy:(NSArray *)arrays
{
    if (!arrays.count) {
        return;
    }
    self.policyArys = [PolicyModel mj_objectArrayWithKeyValuesArray:arrays];
    [Util saveNSUserDefaultsForObject:arrays forKey:PolicyKEY];
}

//保存附件下载id
- (void)saveAttachment:(NSDictionary *)dict
{
    if (![[dict allKeys] count]) {
        return;
    }
    [Util saveNSUserDefaultsForObject:dict forKey:self.account.userid];
}

//根据id 判断是否下载附件 ID 政策id  index  对应的附件顺序
- (BOOL)judgeFJIsDownloadWIthFJID:(NSString *)fjID
{
    BOOL isload = NO;
    NSDictionary *dict = [Util getDataForKey:self.account.userid.length > 0 ? self.account.userid : @""];
    NSArray *arys = dict[@"id"];
    NSLog(@"里面有的ID-------------------%@",arys);
    if ([[dict allKeys] count]) {
        for (int i = 0; i < arys.count; i++) {
            if ([arys[i] isEqualToString:fjID]) {
                isload = YES;
            }
        }
    }
    return isload;
}
@end
